package com.zxy.ziems.collector.controller;

import com.zxy.ziems.collector.JfxCollectorApplication;
import com.zxy.ziems.collector.application.StageManager;
import com.zxy.ziems.collector.config.CollectorProperties;
import com.zxy.ziems.collector.progress.ProgressHUD;
import com.zxy.ziems.collector.service.ProcessConfigurationService;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBox;
import javafx.stage.WindowEvent;
import net.rgielen.fxweaver.core.FxControllerAndView;
import net.rgielen.fxweaver.core.FxWeaver;
import net.rgielen.fxweaver.core.FxmlView;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author chdzq
 * @create 2024/4/07
 */
@Component
@FxmlView // equal to: @FxmlView("MainController.fxml")
public class MainController {
    private static final Logger logger = LoggerFactory.getLogger(JfxCollectorApplication.class);

    private String greeting;
    private final FxWeaver fxWeaver;

    private final StageManager stageManager;

    private final FxControllerAndView<DialogController, VBox> dialog;

    @FXML
    private VBox rootLayout;

    @FXML
    public Label label;
    @FXML
    public Button startButton;

    @FXML ListView<String> listView;

    public ObservableList<String> items =
            FXCollections.observableArrayList();//创建一个客观擦的ArrayList对象

    public MainController(FxWeaver fxWeaver,
                          StageManager stageManager,
                          FxControllerAndView<DialogController, VBox> dialog) {
        this.fxWeaver = fxWeaver;
        this.stageManager = stageManager;
        this.dialog = dialog;
    }

    @FXML
    public void initialize() throws SchedulerException {
        this.label.setText(greeting);
        /*helloButton.setOnAction(
                actionEvent -> this.label.setText(greeting)
        );

        openSimpleDialogButton.setOnAction(
                actionEvent -> dialog.getController().show()
        );

        openTiledDialogButton.setOnAction(
                actionEvent -> {
                    logger.info("点击了");
                    FxControllerAndView<TiledDialogController, VBox> tiledDialog =
                            fxWeaver.load(TiledDialogController.class);
                    tiledDialog.getView().ifPresent(
                            v -> {
                                Label label = new Label();
                                label.setText("Dynamically added Label");
                                v.getChildren().add(label);
                            }
                    );
                    tiledDialog.getController().show();
                }
        );

        openProgressButton.setOnAction(
                actionEvent -> {
                    logger.info("点击了");
                    ProgressHUD.of(
                            (Stage) rootLayout.getScene().getWindow(),
                            new Task<>() {
                                @Override
                                protected Object call() throws Exception {
                                    Result<Config> config = request.getConfig("1772159406391754752");
                                    logger.info(JSONUtil.toJsonStr(config));
                                    return config;
                                }
                            },
                            "正在请求接口"
                    ).show();
                });*/

        listView.setItems(items);

        initData();
        stageManager.getPrimaryStage().setOnShown(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent windowEvent) {
                requestConfig();
            }
        });
        startButton.setOnAction(
                actionEvent -> {
                    logger.info("点击了");
                    FxControllerAndView<TiledDialogController, VBox> tiledDialog =
                            fxWeaver.load(TiledDialogController.class);
                    tiledDialog.getView().ifPresent(
                            v -> {
                                Label label = new Label();
                                label.setText("Dynamically added Label");
                                v.getChildren().add(label);
                            }
                    );
                    tiledDialog.getController().show();
                }
        );
    }

    private void initData() {
        greeting = collectorProperties.getTitle();
        items.add("选项");
    }

    private void requestConfig() {
        ProgressHUD.of(
                stageManager.getPrimaryStage(),
                new Task<>() {
                    @Override
                    protected Object call() throws Exception {
                        service.requestConfig();
                        return 0;
                    }
                },
                "正在请求配置接口"
                ).show();
    }

    private CollectorProperties collectorProperties;

    @Autowired
    public void setCollectorProperties(CollectorProperties collectorProperties) {
        this.collectorProperties = collectorProperties;
    }

    private ProcessConfigurationService service;

    @Autowired
    public void setService(ProcessConfigurationService processConfigurationService) {
        this.service = processConfigurationService;
    }
}
